package cn.micai.base.io;

import java.io.*;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/20 18:10
 */
public class InputStreamToStringExample {

    public static void main(String [] args) {

        // intilize an InputStream
        InputStream is = new ByteArrayInputStream("file content..blah blah".getBytes());

        String result = getStringFromInputStream(is);

        System.out.println(result);
        System.out.println("Done");
    }

    // convert InputStream to String
    private static String getStringFromInputStream(InputStream is) {
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();

        String line;
        try {
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
}
